package self.zhangkang.chart

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject
import self.zhangkang.chart.data.KLineInfo
import self.zhangkang.chart.service.index.BIASTools
import self.zhangkang.chart.service.index.CCITools
import self.zhangkang.chart.service.index.KDJTools
import self.zhangkang.chart.service.index.MACDTools
import self.zhangkang.chart.service.index.MATools
import self.zhangkang.chart.service.index.RSITools
import self.zhangkang.chart.service.index.WRTools
import self.zhangkang.chart.service.main.BOLLTools

/**
 * @author：zhangkang19164
 * created on：2025/3/20 15:10
 * description：
 */
class KLineViewModel(application: Application) : AndroidViewModel(application) {

    private var mCurrentStock: Stock? = null

    private val mKLineLiveData = MutableLiveData<List<KLineInfo>>()
    val kLineLiveData: LiveData<List<KLineInfo>> = mKLineLiveData

    fun setStock(stock: Stock?) {
        mCurrentStock = stock
        if (null == stock) {
            clearData()
        } else {
            loadData(stock)
        }
    }

    private fun loadData(stock: Stock) {
        if ("600000" == stock.code && "SH" == stock.codeType) {
            viewModelScope.launch(Dispatchers.IO) {
                getApplication<Application>().assets.open("SH600000_kline.json").bufferedReader()
                    .use {
                        val jsonObject = JSONObject(it.readText())
                        val info = jsonObject.getJSONObject("info")
                        val dataList = info.getJSONArray("datas")
                        val preClose = info.getDouble("PC").toFloat()
                        val length = dataList.length()
                        val resultList = mutableListOf<KlineQuote>()
                        repeat(length) { index ->
                            val dataInfo = dataList.getJSONObject(index)
                            val klineQuote = KlineQuote(
                                T = dataInfo.getString("T"),
                                O = dataInfo.getString("O"),
                                C = dataInfo.getString("C"),
                                V = dataInfo.optString("V"),
                                VL = dataInfo.getString("VL"),
                                AM = dataInfo.getString("AM"),
                                H = dataInfo.getString("H"),
                                L = dataInfo.getString("L"),
                                TR = dataInfo.getString("TR")
                            )
                            resultList.add(klineQuote)
                        }
                        resultList.reverse()
                        val klineData = resultList.map { klineQuote ->
                            DataTools.kLineQuoteToKLineInfo(
                                klineQuote = klineQuote,
                                preCLosePrice = preClose
                            )
                        }.toMutableList()
                        //计算各种指标
                        MATools.calculationPriceMA(klineData)
                        MATools.calculationVolumeMA(klineData)
                        KDJTools.calculationKDJ(klineData)
                        MACDTools.calculationMACD(klineData)
                        RSITools.calculationRSI(klineData)
                        CCITools.calculationCCI(klineData)
                        WRTools.calculationWR(klineData)
                        BIASTools.calculationBIAS(klineData)
                        BOLLTools.calculationBOLL(klineData)

                        mKLineLiveData.postValue(klineData)
                    }
            }
        }

    }

    private fun clearData() {
        mKLineLiveData.postValue(emptyList())
    }

}